/*
 * @Description: 接收登记添加、编辑通用表单
 * @Author: Rfan
 * @Date: 2022-11-23 18:17:54
 * @LastEditTime: 2023-01-17 17:31:34
 */

import { useEffect, useState } from 'react';
import { Form, Input, InputNumber, Select } from 'antd';
import { useAccessionContext } from '../../../contexts/AccessionContext';
import fondService from '../../../../../../domains/fond/fondService';
import { appState } from '@/store/mobx';
import Fond from '@/domains/fond/entities/fond';
import archiveTypeService from '@/domains/archiveType/archiveTypeService';
import ArchiveType from '@/domains/archiveType/entities/archiveType';
import type { IFond } from '@/interfaces/fond';
import type { IArchiveType } from '@/interfaces/archiveType';
import type { IBaseDic } from '@/interfaces/global';
import JPDatePicker from '@/components/JPComponents/JPDatePicker';

interface ITransferFormProps {
  visible: boolean;
}

const AccessionForm = (props: ITransferFormProps) => {
  const { visible } = props;
  const { resultsStatusList, accessionTypeList } = useAccessionContext();
  const form = Form.useFormInstance();
  const selectFond = Form.useWatch('fondsId', form);

  // 全宗列表
  const [fondList, setFondList] = useState<IFond[]>([]);
  // 档案类型列表
  const [ATList, setATList] = useState<IArchiveType[]>([]);

  useEffect(() => {
    if (visible) {
      fondService.getFondList(appState.unitId).then((list) => {
        setFondList(list.map((item: Fond) => ({ value: item.id, label: item.fondName })));
      });
    }
  }, [visible]);

  useEffect(() => {
    if (selectFond) {
      archiveTypeService.getATList(selectFond).then((list) => {
        setATList(list.map((item: ArchiveType) => ({ value: item.id, label: item.atName })));
      });
    }
  }, [selectFond]);

  return (
    <>
      <Form.Item name="registrationTime" label="登记时间" rules={[{ required: true }]}>
        <JPDatePicker />
      </Form.Item>
      <Form.Item name="year" label="年度" rules={[{ required: true }]}>
        <Input allowClear />
      </Form.Item>
      <Form.Item name="fondsId" label="全宗">
        <Select options={fondList} />
      </Form.Item>
      <Form.Item name="archiveTypeId" label="档案类型">
        <Select options={ATList} />
      </Form.Item>
      <Form.Item name="receiveBatchId" label="批次">
        <Input allowClear />
      </Form.Item>
      <Form.Item name="transferMode" label="交接方式">
        <Select
          options={accessionTypeList.map((item: IBaseDic) => ({
            value: item.id,
            label: item.name
          }))}
        />
      </Form.Item>
      <Form.Item name="folderCount" label="案卷数量">
        <InputNumber />
      </Form.Item>
      <Form.Item name="fileCount" label="文件数量">
        <InputNumber />
      </Form.Item>
      <Form.Item name="efileCount" label="电子文件数量">
        <InputNumber />
      </Form.Item>
      <Form.Item name="transferDataSize" label="移交数据量">
        <InputNumber />
      </Form.Item>
      <Form.Item name="transferVectorType" label="移交载体">
        <Input allowClear />
      </Form.Item>
      <Form.Item name="transferUnit" label="接收单位">
        <Input allowClear />
      </Form.Item>
      <Form.Item name="detectionJobId" label="规范性检测ID">
        <Input allowClear />
      </Form.Item>
      <Form.Item name="detectionResult" label="检测结果">
        <Select
          options={resultsStatusList.map((item: IBaseDic) => ({
            value: item.id,
            label: item.name
          }))}
        />
      </Form.Item>
      <Form.Item name="registrant" label="填表人">
        <Input allowClear />
      </Form.Item>
      <Form.Item label="审核人">
        <Input allowClear />
      </Form.Item>
      <Form.Item name="remark" label="备注">
        <Input.TextArea placeholder="备注信息描述，最多可填写2000字" maxLength={2000} showCount />
      </Form.Item>
    </>
  );
};

export default AccessionForm;
